package com.yang.utils;

import com.yang.service.NoteEmailService;
import com.yang.entity.EmailSmsCountInfo;
import com.yang.utils.info.PictureDataInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author yangdacheng
 * @title: PictureFactory
 * @projectName note_api
 * @description: TODO
 * @date 2022/3/25 11:13 上午
 */
@Component
public class PictureFactory {

    @Resource
    private NoteEmailService noteEmailService;

    /**
     * 查询图表1数据
     */
    public List<EmailSmsCountInfo> queryOneData(String userID){
        Calendar calendar = Calendar.getInstance(); //获取日历对象
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日"); //获取时间格式化对象
        String label = "";
        List<EmailSmsCountInfo> pictureOne = new ArrayList<>();
        for (int index=1;index<=7;index++){
            calendar.add(calendar.DATE,-1); //获取昨天日期
            label = dateFormat.format(calendar.getTime());
            EmailSmsCountInfo emailSmsCountInfo = new EmailSmsCountInfo(label,
                    noteEmailService.queryDayCountByState(userID, 0, index),
                    noteEmailService.queryDayCountByState(userID, 1, index));
            pictureOne.add(emailSmsCountInfo);
        }
        Collections.reverse(pictureOne);  //倒序输出
        return pictureOne;
    }

    /**
     * 格式图表1
     * @param pictureDataInfos
     * @return
     */
    public List<PictureDataInfo> formatList(List<PictureDataInfo> pictureDataInfos){
        List<PictureDataInfo> newList = new ArrayList<>();
        Map<String,Integer> map = new HashMap<>();
        //获取集合中重复的元素个数
        for (PictureDataInfo pictureDataInfo : pictureDataInfos) {
            Integer index = 1; //定义一个计数器，用来记录重复数据的个数
            if (map.get(pictureDataInfo.getName()) != null){
                index = map.get(pictureDataInfo.getName()) + 1;
            }
            map.put(pictureDataInfo.getName(),index);
        }
        map.forEach((key,value)->{
            PictureDataInfo pictureDataInfo = new PictureDataInfo();
            pictureDataInfo.setName(key).setValue(value);
            newList.add(pictureDataInfo);
        });
        return newList;
    }
}
